﻿package com.mixmedia.view {
	import flash.display.SimpleButton;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import com.mixmedia.view.events.ButtonClipEvent;	

	/**
	 * @author Colin
	 */
	final public class ButtonEvt extends EventDispatcher implements IButtonClip, IFASTEventDispatcher{
		private var _isHighlight:Boolean = false;
		private var hitArea: SimpleButton;
		private var overDelay : Number=0;
		private var outDelay:Number=0;
		private var overIID:Number;
		private var outIID:Number;
		private var elements : Array=[];
	
		public function ButtonEvt(hitarea:SimpleButton){
			this.hitArea = hitarea;
			hitArea.addEventListener(MouseEvent.MOUSE_OVER, over, false, 0,true);
			hitArea.addEventListener(MouseEvent.MOUSE_OUT,  out,false, 0,true);
			hitArea.addEventListener(MouseEvent.MOUSE_UP,   click, false,0,true);
			hitArea.addEventListener(MouseEvent.MOUSE_DOWN, down, false, 0, true);
		}

		public function addElement(element:IButtonElement):void{
			elements.push(element);
			this.when(ButtonClipEvent.MOUSE_OVER , element, element.buttonOver);
			this.when(ButtonClipEvent.MOUSE_OUT  , element, element.buttonOut);
			this.when(ButtonClipEvent.MOUSE_DOWN , element, element.buttonDown);
			this.when(ButtonClipEvent.RESET      , element, element.buttonReset);
		}

		public function getElements():Array{
			return elements;
		}

		private function clearIID():void{
			clearTimeout(overIID);
			clearTimeout(outIID);
		}

		private function out(...e):void{
			clearIID();
			if(outDelay==0){
				doOut();
			}else{
				outIID = setTimeout(doOut,outDelay);
			}
		}
		
		private function doOut():void{
			dispatchEvent(new ButtonClipEvent(ButtonClipEvent.MOUSE_OUT , isHighlight));
			dispatchEvent(new ButtonClipEvent(ButtonClipEvent.ROLL_OUT  , isHighlight));
		}
	
		private function over(...e):void{
			clearIID();
			if(overDelay==0){
				doOver();
			}else{
				overIID = setTimeout(doOver,overDelay);
			}
		}
	
		private function doOver():void{
			clearIID();
			dispatchEvent(new ButtonClipEvent(ButtonClipEvent.MOUSE_OVER, isHighlight));
			dispatchEvent(new ButtonClipEvent(ButtonClipEvent.ROLL_OVER , isHighlight));	
		}
		
		private function reset():void{
			dispatchEvent(new ButtonClipEvent(ButtonClipEvent.RESET     , isHighlight));
		}
		
		private function click(...e):void{
			dispatchEvent(new ButtonClipEvent(ButtonClipEvent.MOUSE_UP	, isHighlight));
			dispatchEvent(new ButtonClipEvent(ButtonClipEvent.CLICK		, isHighlight));
		}
	
		private function down(...e):void{
			dispatchEvent(new ButtonClipEvent(ButtonClipEvent.MOUSE_DOWN, isHighlight));
		}
		
		public function get isHighlight():Boolean{
			return _isHighlight;
		}
		
		public function set isHighlight(value:Boolean):void{
			_isHighlight = value;
			reset();
		}		
		public function getBase():SimpleButton{
			return hitArea;
		}

		public function select() : void {
			isHighlight = true;
			reset();
			dispatchEvent(new ButtonClipEvent(ButtonClipEvent.SELECT, isHighlight));
		}

		public function setMouseOverDelay(num : Number) : void {
			overDelay = num;
		}
		
		public function setMouseOutDelay(num : Number) : void {
			outDelay = num;
		}
	
		public function clearMouseOver() : void {
			clearTimeout(overIID);
		}
	
		public function clearMouseOut() : void {
			clearTimeout(outIID);
		}

		public function when(eventType : String, whichObject : Object, callFunction : Function) : void {
			addEventListener(eventType, callFunction, false, 0, true);
		}
	}
}